// components/datePicker/index.js
const App = getApp();
const { padStart } = require('../../js/newUtil.js');
const dateTimePicker = require('../utils/datePicker.js')
const componentBehavior = require('../../js/common/ComponentBehavior')

Component({
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    placeholder:{
      value: "",
      type: String
    },
    startDateTime:{
      value: "",
      type: String
    },
    endDateTime:{
      value: "",
      type: String
    },
    pText:{
      value: "",
      type: String
    },
    // 默认日期时间类型，即初次打开时显示的日期时间，default: 传入的时间 now: 当前时间
    defaultValueType: {
      value: "default",
      type: String
    },
    fontSizeClass: {
      value: 'ft-big',
      type: String
    }
  },
  observers:{
    'startDateTime':function(startDateTime){
      this.initData()
    },
    'endDateTime':function(endDateTime){
      this.initData()
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    dateTimeArray: null,
    dateTime: null,
    dateTimeWhole: '',
  },
  lifetimes: {
    attached: function () {
      this.initData()
    }
  },
  pageLifetimes: {
    show: function() {
      this.initData()
    },
    hide: function() {
      // 页面被隐藏
    },
    resize: function(size) {
      // 页面尺寸变化
    }
  },
  behaviors: [componentBehavior],
  /**
   * 组件的方法列表
   */
  methods: {
    initData(date) {
      let defaultValueType = this.data.defaultValueType
      // 获取完整的年月日 时分秒，以及默认显示的数组
      this.data.unit = ['年', '月', '日', '时', '分']
      if (defaultValueType == 'now') {
        let now = this.getNow()
        this.data.dateTimePicker = dateTimePicker.newDateTimePicker(this.data.startDateTime, this.data.endDateTime, now)
      } else {
        this.data.dateTimePicker = dateTimePicker.newDateTimePicker(this.data.startDateTime, this.data.endDateTime, this.data.pText)
      }
      let obj = this.data.dateTimePicker.render();
      let lastArray = obj.dateTimeArray;
      let lastTime = obj.dateTime;
      for (let i = 0; i < lastArray.length; i++) {
        lastArray[i] = lastArray[i].map(m => m + this.data.unit[i])
      }
      this.data.dateTimeArray = lastArray
      this.data.dateTime = lastTime
      this.setData({
        dateTimeArray: this.data.dateTimeArray,
        dateTime: this.data.dateTime
      })
    },
    changeDateTime(e) { 
      this.data.dateTime = e.detail.value
      const year = this.data.dateTimeArray[0][this.data.dateTime[0]].replace(/年/, '')
      const month = this.data.dateTimeArray[1][this.data.dateTime[1]].replace(/月/, '')
      const day = this.data.dateTimeArray[2][this.data.dateTime[2]].replace(/日/, '')
      const hour = this.data.dateTimeArray[3][this.data.dateTime[3]].replace(/时/, '')
      const minute = this.data.dateTimeArray[4][this.data.dateTime[4]].replace(/分/, '')
      this.data.dateTimeWhole = `${year}-${month}-${day} ${hour}:${minute}`
      this.setData({
        dateTimeWhole: this.data.dateTimeWhole,
      })
      console.log(this.data.dateTimeWhole)
      this.triggerEvent('getDateString', this.data.dateTimeWhole)
    },
    changeDateTimeColumn(e) {
      const { column, value } = e.detail
      // this.$set(this.data.dateTime, column, value)
      let dateTimeTemp = 'dateTime['+column+']'
      this.setData({
        [dateTimeTemp]: value
      })
      this.data.dateTimePicker.setValue({ dateTimeArray: this.data.dateTimeArray, dateTime: this.data.dateTime })
      for (let i = 1; i < this.data.dateTime.length; i++) {
        if (column == i - 1) {
          for (let j = i; j < this.data.dateTime.length; j++) {
            // this.$set(this.data.dateTime, j, 0)
            let temp = 'dateTime['+j+']'
            this.setData({
              [temp]: 0
            })
          }
        }
        let arr = this.data.dateTimePicker.dispatch(i).map(m => m + this.data.unit[i])
        // this.$set(this.data.dateTimeArray, i, arr)
        let temp1 = 'dateTimeArray['+i+']'
        this.setData({
          [temp1]: arr
        })
      }
      this.setData({
        dateTimeArray: this.data.dateTimeArray,
        dateTime: this.data.dateTime
      })
    },
    /**
     * 获取当前时间
     */
    getNow () {
      let now = new Date()
      let year = now.getFullYear()
      let month = now.getMonth() + 1
      let date = now.getDate()
      let hour = now.getHours()
      let min = now.getMinutes()
      let sec = '00'
      month = padStart(month, 2, '0')
      date = padStart(date, 2, '0')
      hour = padStart(hour, 2, '0')
      min = padStart(min, 2, '0')
      return `${year}-${month}-${date} ${hour}:${min}:${sec}`
    },
    /**
     * 清空当前选择的时间
     */
    clear () {
      this.setData({
        dateTimeWhole: ''
      })
    }
  }
})